home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / imageop.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  14.5 KB  |  740 lines

  1. /* imageopmodule - Various operations on pictures */
  2.  
  3. #ifdef sun
  4. #define signed
  5. #endif
  6.  
  7. #include "Python.h"
  8.  
  9. #if SIZEOF_INT == 4
  10. typedef int Py_Int32;
  11. typedef unsigned int Py_UInt32;
  12. #else
  13. #if SIZEOF_LONG == 4
  14. typedef long Py_Int32;
  15. typedef unsigned long Py_UInt32;
  16. #else
  17. #error "No 4-byte integral type"
  18. #endif
  19. #endif
  20.  
  21. #define CHARP(cp, xmax, x, y) ((char *)(cp+y*xmax+x))
  22. #define SHORTP(cp, xmax, x, y) ((short *)(cp+2*(y*xmax+x)))
  23. #define LONGP(cp, xmax, x, y) ((Py_Int32 *)(cp+4*(y*xmax+x)))
  24.  
  25. static PyObject *ImageopError;
  26.  
  27. static PyObject *
  28. imageop_crop(self, args)
  29.     PyObject *self;
  30. PyObject *args;
  31. {
  32.     char *cp, *ncp;
  33.     short *nsp;
  34.     Py_Int32 *nlp;
  35.     int len, size, x, y, newx1, newx2, newy1, newy2;
  36.     int ix, iy, xstep, ystep;
  37.     PyObject *rv;
  38.  
  39.     if ( !PyArg_Parse(args, "(s#iiiiiii)", &cp, &len, &size, &x, &y,
  40.               &newx1, &newy1, &newx2, &newy2) )
  41.         return 0;
  42.     
  43.     if ( size != 1 && size != 2 && size != 4 ) {
  44.         PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
  45.         return 0;
  46.     }
  47.     if ( len != size*x*y ) {
  48.         PyErr_SetString(ImageopError, "String has incorrect length");
  49.         return 0;
  50.     }
  51.     xstep = (newx1 < newx2)? 1 : -1;
  52.     ystep = (newy1 < newy2)? 1 : -1;
  53.     
  54.     rv = PyString_FromStringAndSize(NULL,
  55.                  (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size);
  56.     if ( rv == 0 )
  57.         return 0;
  58.     ncp = (char *)PyString_AsString(rv);
  59.     nsp = (short *)ncp;
  60.     nlp = (Py_Int32 *)ncp;
  61.     newy2 += ystep;
  62.     newx2 += xstep;
  63.     for( iy = newy1; iy != newy2; iy+=ystep ) {
  64.         for ( ix = newx1; ix != newx2; ix+=xstep ) {
  65.             if ( iy < 0 || iy >= y || ix < 0 || ix >= x ) {
  66.                 if ( size == 1 )
  67.                     *ncp++ = 0;
  68.                 else
  69.                     *nlp++ = 0;
  70.             } else {
  71.                 if ( size == 1 )
  72.                     *ncp++ = *CHARP(cp, x, ix, iy);
  73.                 else if ( size == 2 )
  74.                     *nsp++ = *SHORTP(cp, x, ix, iy);
  75.                 else
  76.                     *nlp++ = *LONGP(cp, x, ix, iy);
  77.             }
  78.         }
  79.     }
  80.     return rv;
  81. }
  82.  
  83. static PyObject *
  84. imageop_scale(self, args)
  85.     PyObject *self;
  86. PyObject *args;
  87. {
  88.     char *cp, *ncp;
  89.     short *nsp;
  90.     Py_Int32 *nlp;
  91.     int len, size, x, y, newx, newy;
  92.     int ix, iy;
  93.     int oix, oiy;
  94.     PyObject *rv;
  95.  
  96.     if ( !PyArg_Parse(args, "(s#iiiii)",
  97.               &cp, &len, &size, &x, &y, &newx, &newy) )
  98.         return 0;
  99.     
  100.     if ( size != 1 && size != 2 && size != 4 ) {
  101.         PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
  102.         return 0;
  103.     }
  104.     if ( len != size*x*y ) {
  105.         PyErr_SetString(ImageopError, "String has incorrect length");
  106.         return 0;
  107.     }
  108.     
  109.     rv = PyString_FromStringAndSize(NULL, newx*newy*size);
  110.     if ( rv == 0 )
  111.         return 0;
  112.     ncp = (char *)PyString_AsString(rv);
  113.     nsp = (short *)ncp;
  114.     nlp = (Py_Int32 *)ncp;
  115.     for( iy = 0; iy < newy; iy++ ) {
  116.         for ( ix = 0; ix < newx; ix++ ) {
  117.             oix = ix * x / newx;
  118.             oiy = iy * y / newy;
  119.             if ( size == 1 )
  120.                 *ncp++ = *CHARP(cp, x, oix, oiy);
  121.             else if ( size == 2 )
  122.                 *nsp++ = *SHORTP(cp, x, oix, oiy);
  123.             else
  124.                 *nlp++ = *LONGP(cp, x, oix, oiy);
  125.         }
  126.     }
  127.     return rv;
  128. }
  129.  
  130. /* Note: this routine can use a bit of optimizing */
  131.  
  132. static PyObject *
  133. imageop_tovideo(self, args)
  134.     PyObject *self;
  135. PyObject *args;
  136. {
  137.     int maxx, maxy, x, y, len;
  138.     int i;
  139.     unsigned char *cp, *ncp;
  140.     int width;
  141.     PyObject *rv;
  142.    
  143.     
  144.     if ( !PyArg_Parse(args, "(s#iii)", &cp, &len, &width, &maxx, &maxy) )
  145.         return 0;
  146.  
  147.     if ( width != 1 && width != 4 ) {
  148.         PyErr_SetString(ImageopError, "Size should be 1 or 4");
  149.         return 0;
  150.     }
  151.     if ( maxx*maxy*width != len ) {
  152.         PyErr_SetString(ImageopError, "String has incorrect length");
  153.         return 0;
  154.     }
  155.     
  156.     rv = PyString_FromStringAndSize(NULL, len);
  157.     if ( rv == 0 )
  158.         return 0;
  159.     ncp = (unsigned char *)PyString_AsString(rv);
  160.  
  161.     if ( width == 1 ) {
  162.         memcpy(ncp, cp, maxx);        /* Copy first line */
  163.         ncp += maxx;
  164.         for (y=1; y<maxy; y++) {    /* Interpolate other lines */
  165.             for(x=0; x<maxx; x++) {
  166.                 i = y*maxx + x;
  167.                 *ncp++ = ((int)cp[i] + (int)cp[i-maxx]) >> 1;
  168.             }
  169.         }
  170.     } else {
  171.         memcpy(ncp, cp, maxx*4);        /* Copy first line */
  172.         ncp += maxx*4;
  173.         for (y=1; y<maxy; y++) {    /* Interpolate other lines */
  174.             for(x=0; x<maxx; x++) {
  175.                 i = (y*maxx + x)*4 + 1;
  176.                 *ncp++ = 0;    /* Skip alfa comp */
  177.                 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
  178.                 i++;
  179.                 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
  180.                 i++;
  181.                 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
  182.             }
  183.         }
  184.     }
  185.     return rv;
  186. }
  187.  
  188. static PyObject *
  189. imageop_grey2mono(self, args)
  190.     PyObject *self;
  191. PyObject *args;
  192. {
  193.     int tres, x, y, len;
  194.     unsigned char *cp, *ncp;
  195.     unsigned char ovalue;
  196.     PyObject *rv;
  197.     int i, bit;
  198.    
  199.     
  200.     if ( !PyArg_Parse(args, "(s#iii)", &cp, &len, &x, &y, &tres) )
  201.         return 0;
  202.  
  203.     if ( x*y != len ) {
  204.         PyErr_SetString(ImageopError, "String has incorrect length");
  205.         return 0;
  206.     }
  207.     
  208.     rv = PyString_FromStringAndSize(NULL, (len+7)/8);
  209.     if ( rv == 0 )
  210.         return 0;
  211.     ncp = (unsigned char *)PyString_AsString(rv);
  212.  
  213.     bit = 0x80;
  214.     ovalue = 0;
  215.     for ( i=0; i < len; i++ ) {
  216.         if ( (int)cp[i] > tres )
  217.             ovalue |= bit;
  218.         bit >>= 1;
  219.         if ( bit == 0 ) {
  220.             *ncp++ = ovalue;
  221.             bit = 0x80;
  222.             ovalue = 0;
  223.         }
  224.     }
  225.     if ( bit != 0x80 )
  226.         *ncp++ = ovalue;
  227.     return rv;
  228. }
  229.  
  230. static PyObject *
  231. imageop_grey2grey4(self, args)
  232.     PyObject *self;
  233. PyObject *args;
  234. {
  235.     int x, y, len;
  236.     unsigned char *cp, *ncp;
  237.     unsigned char ovalue;
  238.     PyObject *rv;
  239.     int i;
  240.     int pos;
  241.    
  242.     
  243.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  244.         return 0;
  245.  
  246.     if ( x*y != len ) {
  247.         PyErr_SetString(ImageopError, "String has incorrect length");
  248.         return 0;
  249.     }
  250.     
  251.     rv = PyString_FromStringAndSize(NULL, (len+1)/2);
  252.     if ( rv == 0 )
  253.         return 0;
  254.     ncp = (unsigned char *)PyString_AsString(rv);
  255.     pos = 0;
  256.     ovalue = 0;
  257.     for ( i=0; i < len; i++ ) {
  258.         ovalue |= ((int)cp[i] & 0xf0) >> pos;
  259.         pos += 4;
  260.         if ( pos == 8 ) {
  261.             *ncp++ = ovalue;
  262.             ovalue = 0;
  263.             pos = 0;
  264.         }
  265.     }
  266.     if ( pos != 0 )
  267.         *ncp++ = ovalue;
  268.     return rv;
  269. }
  270.  
  271. static PyObject *
  272. imageop_grey2grey2(self, args)
  273.     PyObject *self;
  274. PyObject *args;
  275. {
  276.     int x, y, len;
  277.     unsigned char *cp, *ncp;
  278.     unsigned char ovalue;
  279.     PyObject *rv;
  280.     int i;
  281.     int pos;
  282.    
  283.     
  284.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  285.         return 0;
  286.  
  287.     if ( x*y != len ) {
  288.         PyErr_SetString(ImageopError, "String has incorrect length");
  289.         return 0;
  290.     }
  291.     
  292.     rv = PyString_FromStringAndSize(NULL, (len+3)/4);
  293.     if ( rv == 0 )
  294.         return 0;
  295.     ncp = (unsigned char *)PyString_AsString(rv);
  296.     pos = 0;
  297.     ovalue = 0;
  298.     for ( i=0; i < len; i++ ) {
  299.         ovalue |= ((int)cp[i] & 0xc0) >> pos;
  300.         pos += 2;
  301.         if ( pos == 8 ) {
  302.             *ncp++ = ovalue;
  303.             ovalue = 0;
  304.             pos = 0;
  305.         }
  306.     }
  307.     if ( pos != 0 )
  308.         *ncp++ = ovalue;
  309.     return rv;
  310. }
  311.  
  312. static PyObject *
  313. imageop_dither2mono(self, args)
  314.     PyObject *self;
  315. PyObject *args;
  316. {
  317.     int sum, x, y, len;
  318.     unsigned char *cp, *ncp;
  319.     unsigned char ovalue;
  320.     PyObject *rv;
  321.     int i, bit;
  322.    
  323.     
  324.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  325.         return 0;
  326.  
  327.     if ( x*y != len ) {
  328.         PyErr_SetString(ImageopError, "String has incorrect length");
  329.         return 0;
  330.     }
  331.     
  332.     rv = PyString_FromStringAndSize(NULL, (len+7)/8);
  333.     if ( rv == 0 )
  334.         return 0;
  335.     ncp = (unsigned char *)PyString_AsString(rv);
  336.  
  337.     bit = 0x80;
  338.     ovalue = 0;
  339.     sum = 0;
  340.     for ( i=0; i < len; i++ ) {
  341.         sum += cp[i];
  342.         if ( sum >= 256 ) {
  343.             sum -= 256;
  344.             ovalue |= bit;
  345.         }
  346.         bit >>= 1;
  347.         if ( bit == 0 ) {
  348.             *ncp++ = ovalue;
  349.             bit = 0x80;
  350.             ovalue = 0;
  351.         }
  352.     }
  353.     if ( bit != 0x80 )
  354.         *ncp++ = ovalue;
  355.     return rv;
  356. }
  357.  
  358. static PyObject *
  359. imageop_dither2grey2(self, args)
  360.     PyObject *self;
  361. PyObject *args;
  362. {
  363.     int x, y, len;
  364.     unsigned char *cp, *ncp;
  365.     unsigned char ovalue;
  366.     PyObject *rv;
  367.     int i;
  368.     int pos;
  369.     int sum = 0, nvalue;
  370.    
  371.     
  372.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  373.         return 0;
  374.  
  375.     if ( x*y != len ) {
  376.         PyErr_SetString(ImageopError, "String has incorrect length");
  377.         return 0;
  378.     }
  379.     
  380.     rv = PyString_FromStringAndSize(NULL, (len+3)/4);
  381.     if ( rv == 0 )
  382.         return 0;
  383.     ncp = (unsigned char *)PyString_AsString(rv);
  384.     pos = 1;
  385.     ovalue = 0;
  386.     for ( i=0; i < len; i++ ) {
  387.         sum += cp[i];
  388.         nvalue = sum & 0x180;
  389.         sum -= nvalue;
  390.         ovalue |= nvalue >> pos;
  391.         pos += 2;
  392.         if ( pos == 9 ) {
  393.             *ncp++ = ovalue;
  394.             ovalue = 0;
  395.             pos = 1;
  396.         }
  397.     }
  398.     if ( pos != 0 )
  399.         *ncp++ = ovalue;
  400.     return rv;
  401. }
  402.  
  403. static PyObject *
  404. imageop_mono2grey(self, args)
  405.     PyObject *self;
  406. PyObject *args;
  407. {
  408.     int v0, v1, x, y, len, nlen;
  409.     unsigned char *cp, *ncp;
  410.     PyObject *rv;
  411.     int i, bit;
  412.     
  413.     if ( !PyArg_Parse(args, "(s#iiii)", &cp, &len, &x, &y, &v0, &v1) )
  414.         return 0;
  415.  
  416.     nlen = x*y;
  417.     if ( (nlen+7)/8 != len ) {
  418.         PyErr_SetString(ImageopError, "String has incorrect length");
  419.         return 0;
  420.     }
  421.     
  422.     rv = PyString_FromStringAndSize(NULL, nlen);
  423.     if ( rv == 0 )
  424.         return 0;
  425.     ncp = (unsigned char *)PyString_AsString(rv);
  426.  
  427.     bit = 0x80;
  428.     for ( i=0; i < nlen; i++ ) {
  429.         if ( *cp & bit )
  430.             *ncp++ = v1;
  431.         else
  432.             *ncp++ = v0;
  433.         bit >>= 1;
  434.         if ( bit == 0 ) {
  435.             bit = 0x80;
  436.             cp++;
  437.         }
  438.     }
  439.     return rv;
  440. }
  441.  
  442. static PyObject *
  443. imageop_grey22grey(self, args)
  444.     PyObject *self;
  445. PyObject *args;
  446. {
  447.     int x, y, len, nlen;
  448.     unsigned char *cp, *ncp;
  449.     PyObject *rv;
  450.     int i, pos, value = 0, nvalue;
  451.     
  452.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  453.         return 0;
  454.  
  455.     nlen = x*y;
  456.     if ( (nlen+3)/4 != len ) {
  457.         PyErr_SetString(ImageopError, "String has incorrect length");
  458.         return 0;
  459.     }
  460.     
  461.     rv = PyString_FromStringAndSize(NULL, nlen);
  462.     if ( rv == 0 )
  463.         return 0;
  464.     ncp = (unsigned char *)PyString_AsString(rv);
  465.  
  466.     pos = 0;
  467.     for ( i=0; i < nlen; i++ ) {
  468.         if ( pos == 0 ) {
  469.             value = *cp++;
  470.             pos = 8;
  471.         }
  472.         pos -= 2;
  473.         nvalue = (value >> pos) & 0x03;
  474.         *ncp++ = nvalue | (nvalue << 2) |
  475.              (nvalue << 4) | (nvalue << 6);
  476.     }
  477.     return rv;
  478. }
  479.  
  480. static PyObject *
  481. imageop_grey42grey(self, args)
  482.     PyObject *self;
  483. PyObject *args;
  484. {
  485.     int x, y, len, nlen;
  486.     unsigned char *cp, *ncp;
  487.     PyObject *rv;
  488.     int i, pos, value = 0, nvalue;
  489.     
  490.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  491.         return 0;
  492.  
  493.     nlen = x*y;
  494.     if ( (nlen+1)/2 != len ) {
  495.         PyErr_SetString(ImageopError, "String has incorrect length");
  496.         return 0;
  497.     }
  498.     
  499.     rv = PyString_FromStringAndSize(NULL, nlen);
  500.     if ( rv == 0 )
  501.         return 0;
  502.     ncp = (unsigned char *)PyString_AsString(rv);
  503.  
  504.     pos = 0;
  505.     for ( i=0; i < nlen; i++ ) {
  506.         if ( pos == 0 ) {
  507.             value = *cp++;
  508.             pos = 8;
  509.         }
  510.         pos -= 4;
  511.         nvalue = (value >> pos) & 0x0f;
  512.         *ncp++ = nvalue | (nvalue << 4);
  513.     }
  514.     return rv;
  515. }
  516.  
  517. static PyObject *
  518. imageop_rgb2rgb8(self, args)
  519.     PyObject *self;
  520. PyObject *args;
  521. {
  522.     int x, y, len, nlen;
  523.     Py_UInt32 *cp;
  524.     unsigned char *ncp;
  525.     PyObject *rv;
  526.     int i, r, g, b;
  527.     Py_UInt32 value, nvalue;
  528.     
  529.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  530.         return 0;
  531.  
  532.     nlen = x*y;
  533.     if ( nlen*4 != len ) {
  534.         PyErr_SetString(ImageopError, "String has incorrect length");
  535.         return 0;
  536.     }
  537.     
  538.     rv = PyString_FromStringAndSize(NULL, nlen);
  539.     if ( rv == 0 )
  540.         return 0;
  541.     ncp = (unsigned char *)PyString_AsString(rv);
  542.  
  543.     for ( i=0; i < nlen; i++ ) {
  544.         /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */
  545.         value = *cp++;
  546. #if 0
  547.         r = (value >>  5) & 7;
  548.         g = (value >> 13) & 7;
  549.         b = (value >> 22) & 3;
  550. #else
  551.         r = (int) ((value & 0xff) / 255. * 7. + .5);
  552.         g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
  553.         b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
  554. #endif
  555.         nvalue = (r<<5) | (b<<3) | g;
  556.         *ncp++ = (unsigned char)nvalue;
  557.     }
  558.     return rv;
  559. }
  560.  
  561. static PyObject *
  562. imageop_rgb82rgb(self, args)
  563.     PyObject *self;
  564. PyObject *args;
  565. {
  566.     int x, y, len, nlen;
  567.     unsigned char *cp;
  568.     Py_UInt32 *ncp;
  569.     PyObject *rv;
  570.     int i, r, g, b;
  571.     Py_UInt32 value, nvalue;
  572.     
  573.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  574.         return 0;
  575.  
  576.     nlen = x*y;
  577.     if ( nlen != len ) {
  578.         PyErr_SetString(ImageopError, "String has incorrect length");
  579.         return 0;
  580.     }
  581.     
  582.     rv = PyString_FromStringAndSize(NULL, nlen*4);
  583.     if ( rv == 0 )
  584.         return 0;
  585.     ncp = (Py_UInt32 *)PyString_AsString(rv);
  586.  
  587.     for ( i=0; i < nlen; i++ ) {
  588.         /* Bits in source: RRRBBGGG
  589.         ** Red and Green are multiplied by 36.5, Blue by 85
  590.         */
  591.         value = *cp++;
  592.         r = (value >> 5) & 7;
  593.         g = (value     ) & 7;
  594.         b = (value >> 3) & 3;
  595.         r = (r<<5) | (r<<3) | (r>>1);
  596.         g = (g<<5) | (g<<3) | (g>>1);
  597.         b = (b<<6) | (b<<4) | (b<<2) | b;
  598.         nvalue = r | (g<<8) | (b<<16);
  599.         *ncp++ = nvalue;
  600.     }
  601.     return rv;
  602. }
  603.  
  604. static PyObject *
  605. imageop_rgb2grey(self, args)
  606.     PyObject *self;
  607. PyObject *args;
  608. {
  609.     int x, y, len, nlen;
  610.     Py_UInt32 *cp;
  611.     unsigned char *ncp;
  612.     PyObject *rv;
  613.     int i, r, g, b;
  614.     Py_UInt32 value, nvalue;
  615.     
  616.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  617.         return 0;
  618.  
  619.     nlen = x*y;
  620.     if ( nlen*4 != len ) {
  621.         PyErr_SetString(ImageopError, "String has incorrect length");
  622.         return 0;
  623.     }
  624.     
  625.     rv = PyString_FromStringAndSize(NULL, nlen);
  626.     if ( rv == 0 )
  627.         return 0;
  628.     ncp = (unsigned char *)PyString_AsString(rv);
  629.  
  630.     for ( i=0; i < nlen; i++ ) {
  631.         value = *cp++;
  632.         r = (value      ) & 0xff;
  633.         g = (value >>  8) & 0xff;
  634.         b = (value >> 16) & 0xff;
  635.         nvalue = (int)(0.30*r + 0.59*g + 0.11*b);
  636.         if ( nvalue > 255 ) nvalue = 255;
  637.         *ncp++ = (unsigned char)nvalue;
  638.     }
  639.     return rv;
  640. }
  641.  
  642. static PyObject *
  643. imageop_grey2rgb(self, args)
  644.     PyObject *self;
  645. PyObject *args;
  646. {
  647.     int x, y, len, nlen;
  648.     unsigned char *cp;
  649.     Py_UInt32 *ncp;
  650.     PyObject *rv;
  651.     int i;
  652.     Py_UInt32 value;
  653.     
  654.     if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
  655.         return 0;
  656.  
  657.     nlen = x*y;
  658.     if ( nlen != len ) {
  659.         PyErr_SetString(ImageopError, "String has incorrect length");
  660.         return 0;
  661.     }
  662.     
  663.     rv = PyString_FromStringAndSize(NULL, nlen*4);
  664.     if ( rv == 0 )
  665.         return 0;
  666.     ncp = (Py_UInt32 *)PyString_AsString(rv);
  667.  
  668.     for ( i=0; i < nlen; i++ ) {
  669.         value = *cp++;
  670.         *ncp++ = value | (value << 8 ) | (value << 16);
  671.     }
  672.     return rv;
  673. }
  674.  
  675. /*
  676. static object *
  677. imageop_mul(self, args)
  678.     object *self;
  679. object *args;
  680. {
  681.     char *cp, *ncp;
  682.     int len, size, x, y;
  683.     object *rv;
  684.     int i;
  685.  
  686.     if ( !getargs(args, "(s#iii)", &cp, &len, &size, &x, &y) )
  687.         return 0;
  688.     
  689.     if ( size != 1 && size != 4 ) {
  690.         err_setstr(ImageopError, "Size should be 1 or 4");
  691.         return 0;
  692.     }
  693.     if ( len != size*x*y ) {
  694.         err_setstr(ImageopError, "String has incorrect length");
  695.         return 0;
  696.     }
  697.     
  698.     rv = newsizedstringobject(NULL, XXXX);
  699.     if ( rv == 0 )
  700.         return 0;
  701.     ncp = (char *)getstringvalue(rv);
  702.     
  703.     
  704.     for ( i=0; i < len; i += size ) {
  705.     }
  706.     return rv;
  707. }
  708. */
  709.  
  710. static PyMethodDef imageop_methods[] = {
  711.     { "crop",        imageop_crop },
  712.     { "scale",        imageop_scale },
  713.     { "grey2mono",            imageop_grey2mono },
  714.     { "grey2grey2",            imageop_grey2grey2 },
  715.     { "grey2grey4",            imageop_grey2grey4 },
  716.     { "dither2mono",    imageop_dither2mono },
  717.     { "dither2grey2",    imageop_dither2grey2 },
  718.     { "mono2grey",            imageop_mono2grey },
  719.     { "grey22grey",            imageop_grey22grey },
  720.     { "grey42grey",            imageop_grey42grey },
  721.     { "tovideo",            imageop_tovideo },
  722.     { "rgb2rgb8",            imageop_rgb2rgb8 },
  723.     { "rgb82rgb",            imageop_rgb82rgb },
  724.     { "rgb2grey",            imageop_rgb2grey },
  725.     { "grey2rgb",            imageop_grey2rgb },
  726.     { 0,                    0 }
  727. };
  728.  
  729.  
  730. DL_EXPORT(void)
  731. initimageop()
  732. {
  733.     PyObject *m, *d;
  734.     m = Py_InitModule("imageop", imageop_methods);
  735.     d = PyModule_GetDict(m);
  736.     ImageopError = PyErr_NewException("imageop.error", NULL, NULL);
  737.     if (ImageopError != NULL)
  738.         PyDict_SetItemString(d, "error", ImageopError);
  739. }
  740.